# This makefile requires GNU make.
BOOKNAME= Go
sources := \
	00book.tex \
	99book.bib \
	99defs.tex \
	preface.tex \
        install.tex \
        gccgo_install.tex \
        go_faq.tex \
        go_for_cpp_programmers.tex \
        go_lang_faq.tex \
        go_mem.tex \
        go_spec.tex \
        go_tutorial.tex \
        effective_go.tex \
        contribute.tex \
        title.tex \
	license.tex

image-sources := \
	feature-branches.dot 

# filter
image-dot := $(filter %.dot,$(image-sources))
image-svg := $(filter %.svg,$(image-sources))
image-png := $(filter %.png,$(image-sources))

image-pdf := $(image-dot:%.dot=%.pdf) $(image-svg:%.svg=%.pdf) $(image-png)
image-html := $(image-dot:%.dot=%.png) $(image-svg:%.svg=%.png) $(image-png)


dist-sources := \
	../html/hgicon.png \
	../html/index.html.var \
	../html/index.en.html

latex-options = \
	-interaction batchmode \
	-output-directory $(dir $(1)) \
	-jobname $(basename $(notdir $(1)))

#all: pdf html 
all: pdf 

pdf: pdf/${BOOKNAME}.pdf

# What: A makefile macro function to 
# $< :
# $@ :
define pdf
	mkdir -p $(dir $@)
	TEXINPUTS=$(dir $<): pdflatex $(call latex-options,$@) $< || (rm -f $@; exit 1)
	cp 99book.bib $(dir $@)
	cd $(dir $@) && bibtex $(basename $(notdir $@))
	cd $(dir $@) && makeindex $(basename $(notdir $@))
	TEXINPUTS=$(dir $<): pdflatex $(call latex-options,$@) $< || (rm -f $@; exit 1)
	TEXINPUTS=$(dir $<): pdflatex $(call latex-options,$@) $< || (rm -f $@; exit 1)
	if grep 'Reference.*undefined' $(@:.pdf=.log); then exit 1; fi
endef


#pdf/${BOOKNAME}.pdf: $(sources) $(image-pdf) 
pdf/${BOOKNAME}.pdf: $(sources) $(image-pdf) 
	$(call pdf)


html: onepage split

onepage: $(htlatex) html/onepage/${BOOKNAME}.html html/onepage/${BOOKNAME}.css $(image-html:%=html/onepage/%)

html/onepage/%: %
	cp $< $@

split: $(htlatex) html/split/${BOOKNAME}.html html/split/${BOOKNAME}.css $(image-html:%=html/split/%)

html/split/%: %
	cp $< $@

# This is a horrible hack to work around the fact that the htlatex
# command in tex4ht is itself a horrible hack.  I really don't want to
# include verbatim the big wad of TeX that is repeated in that script,
# but I've given up and run a hacked copy as htlatex.book here.

define htlatex
	mkdir -p $(dir $(1))
	cp 99book.bib $(dir $(1))
	TEXINPUTS=$(dir $(2)): ./htlatex.book $(2) "bookhtml,html4-uni,$(3)" " -cunihtf -utf8" "$(dir $(1))" "$(call latex-options,$(1))" || (rm -f $(1); exit 1)
	cd $(dir $(1)) && tex4ht -f/$(basename $(notdir $(1))) -cvalidate -cunihtf
	cd $(dir $(1)) && t4ht -f/$(basename $(notdir $(1)))
	./fixhtml.py $(dir $(1))/*.html
	rm $(dir $(1))/${BOOKNAME}.css
endef



html/onepage/${BOOKNAME}.html: $(sources) $(image-html) bookhtml.cfg
	$(call htlatex,$@,$<)


html/split/${BOOKNAME}.html: $(sources) bookhtml.cfg
	$(call htlatex,$@,$<,2)

%.tex: %.html
	html2latex $@ $<

# Produce 90dpi PNGs for the web.

%.png: %.svg
	inkscape -D -e $@ $<

%.svg: %.dot
	dot -Tsvg -o $@ $<

# Produce eps & pdf for the pdf

%.pdf: %.eps
	epstopdf $<

%.eps: %.svg
	inkscape -E $@ $<

%.eps: %.dot
	dot -Tps -o $@ $<


clean: clean_slides
	rm -rf dist html pdf/* \
		$(image-dot:%.dot=%.pdf) \
		$(image-dot:%.dot=%.png) \
		$(image-svg:%.svg=%.pdf) \
		$(image-svg:%.svg=%.png) \
                *~ *.aux *.log 00book.{aux,log,lot,lof,idx,nav,out,snm,toc,pdf}\
                }

clean_slides:
	rm -rf 	slides/*.{aux,log,nav,out,snm,toc,pdf}

install: pdf split $(dist-sources)
	rm -rf dist
	mkdir -p dist
	cp pdf/${BOOKNAME}.pdf dist
	cp html/split/*.{css,html,png} dist
	cp $(dist-sources) dist

rsync: install
	rsync -avz --delete dist sp.red-bean.com:public_html/${BOOKNAME}

upload: all 
	sudo rsync -avz --delete pdf/*.pdf  /var/www/html/books
#	sudo rsync -avz --delete html   /var/www/html/books/
tar: 
	tar czvf /tmp/hobbit-rtfm.tgz  /home/tjyang/docs/hobbit-rtfm
	sudo rsync -avz --delete /tmp/*.tgz /var/www/html/pdf

dia:
	dia -t png xymon-client.dia 
movetmp: 
	sudo rsync -avz --delete pdf/*.pdf  ~tjyang/tmp
test:
	echo $(image-png)

#perl -pi -e 's!\\hline!!' *.tex 
#perl -pi -e 's!\\\\!!' *.tex 
#perl -pi -e 's!\\begin\{document.*!!' *.tex 
#perl -pi -e 's!\\end\{doc.*!!' *.tex 
#perl -pi -e 's!\\title!\\chapter!' *.tex 
#perl -pi -e 's!\\section\*!\\section!' *.tex 
#perl -pi -e 's!\\subsection\*!\\subsection!' *.tex 
#perl -pi -e 's!\\documentclass.*!!' *.tex 
#perl -pi -e 's!\\usepackage.*!!' *.tex 
#perl -pi -e 's!\\setlength.*!!' *.tex 
#perl -pi -e 's!Man page of !!' *.tex 
#perl -pi -e 's!\\subsection!\\section!' *.tex 



